package work.huangxin.share.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import work.huangxin.share.domain.sechand.SechandBrowseHistory;
import work.huangxin.share.domain.sechand.SechandGoods;
import work.huangxin.share.mapper.SechandGoodsMapper;
import work.huangxin.share.service.SechandGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * (SechandGoods) 服务实现类
 *
 * @author 张锦标
 * @since 2023-03-23 10:35:11
 */
@Service
public class SechandGoodsServiceImpl implements SechandGoodsService {

    @Autowired
    private SechandGoodsMapper sechandGoodsMapper;

    @Override
    public List<SechandGoods> selectSechandGoodsByUserId(Integer userId) {
        return sechandGoodsMapper.selectSechandGoodsByUser(userId);
    }

    /**
     * 分页查询
     * @param pageNo,pageSize
     * @return
     */
    @Override
    public PageInfo<SechandGoods> selectPageSechandGoodsList(Integer pageNo, Integer pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        Page<SechandGoods> sechandGoods = sechandGoodsMapper.selectPageSechandGoodsList();
        return PageInfo.of(sechandGoods);
    }

    @Override
    public SechandGoods selectSechandGoodsById(Long sechandGoodsId) {
        return sechandGoodsMapper.selectSechandGoodsById(sechandGoodsId);
    }

    @Override
    public int insertSechandGoods(SechandGoods sechandGoods) {
        return sechandGoodsMapper.insertSelective(sechandGoods);
    }

    @Override
    public int updateSechandGoods(SechandGoods sechandGoods) {
        return sechandGoodsMapper.updateSechand(sechandGoods);
    }

    @Override
    public int deleteSechandGoods(Integer sechandGoodsId,Integer userId) {
        return sechandGoodsMapper.deleteGoodsByUserId(sechandGoodsId,userId);
    }

    @Override
    public int updateGoodsIsBuy(Integer isBuy, Long goodsId) {
        return sechandGoodsMapper.updateIsBuy(isBuy,goodsId);
    }


    @Override
    public PageInfo<SechandGoods> selectSechandGoodsByKeyWord(String keyword, Integer pageNo, Integer pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        List<SechandGoods> sechandGoodsList = sechandGoodsMapper.getGoodsByKeyWord(keyword);
        return PageInfo.of(sechandGoodsList);
    }

    @Override
    public int deleteSechandGoods(Integer sechandGoodsId) {
        return sechandGoodsMapper.deleteGoodsByAdmin(sechandGoodsId);
    }
    @Override
    public PageInfo<SechandGoods> selectGoodByTypeId(Integer pageNo, Integer pageSize, Integer typeId) {
        PageHelper.startPage(pageNo, pageSize);
        List<SechandGoods> sechandGoods = sechandGoodsMapper.selectSechandGoodsByTypeId(typeId);
        PageInfo<SechandGoods> sechandGoodsPageInfo = PageInfo.of(sechandGoods);
        return sechandGoodsPageInfo;
    }

    //得到商品的浏览记录
    @Override
    public PageInfo<SechandBrowseHistory> getBrowseSechandHistory(Integer userId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<SechandBrowseHistory> list = sechandGoodsMapper.getBrowseSechandHistory(userId);
        return PageInfo.of(list);
    }

    //当用户查看商品详情时，将信息添加进数据库中
    @Override
    public int addBrowseHistory(Integer userId, Long goodsId) {
        return sechandGoodsMapper.addBrowseHistory(userId, goodsId);
    }

    //先判断goods_id 是否存在在商品浏览的数据库中
    @Override
    public int brwoseHisotryExist(Long goodsId) {
        return sechandGoodsMapper.brwoseHisotryExist(goodsId);
    }

    //修改历史浏览时间
    @Override
    public int updateBrowseTime(Long goodsId) {
        return sechandGoodsMapper.updateBrowseTime(goodsId);
    }

}

